#include "McuDrivers/uart/uart.h"
#include "McuDrivers/system/system.h"
#include "McuDrivers/flash/DataRecord.h"
#include "McuDrivers/gpio/gpio.h"
#include "McuDrivers/timer/timer.h"
#include "Protocols/Bootloader.h"
#include "Protocols/UserAppFlash.h"
#include "McuDrivers/flash/InnerFlash.h"


uint8_t receiveData[12]={0},dataIndex=0;

void Uart_Tasks();
uint8_t Time_Tasks();
uint8_t Tick_Tasks();

int main(void) 
{    
    uint8_t run=1;
    
    System_Initialize_g();

    Uart_Initial_g(115200);
    //InnerEEPromDataInit();
    System_Delay_MS_g(100);
    //Uart_Printf_g("aaaaaaaa");

    GPIO_LED_D4_Enable_g();
    GPIO_LED_D3_Enable_g();
    Uart_WriteBytes_g(g_Bootloader_EntryBootloaderReponse,12);
//    uint16_t data[4]= {0xffbb,0x00aa,0x5522,0x0011};
//    InnerFlash_WriteTwoWordsFlash(0x00,0xFFF8,data,4);
//    InnerFlash_WriteTwoWordsFlash(0x01,0xFFFA,data,4);
//    uint16_t data[4]= {0xffbb,0x0000,0x5522,0x0000};
//    InnerFlash_WriteTwoWordsFlash(0x02,0xAFF0,data,4);
    while(run)
    {
        Uart_Tasks(); //接收数据处理

        run = Tick_Tasks();
    }
    while(1)
    {
        Uart_Tasks(); //接收数据处理
    }
    return 0;
}

uint16_t countTick = 0;
uint8_t Tick_Tasks()
{
    countTick++;
    //晶振为80MHz
    //每隔800大约1ms
    if(countTick==800)
    {
        countTick=0;
        return Time_Tasks();
    }
    return 1;
}

uint16_t countTimer=0;
uint8_t Time_Tasks()
{
    countTimer++;
    if(countTimer==4000)
    {
        BOOTLOADER_GOTO_8000();
        return 0;
    }
    
    if(countTimer>4000)
        return 0;
    
    if(countTimer%500==0)
        GPIO_LED_D3_Toggle_g();
    
    return 1;
}

//串口接收任务
void Uart_Tasks()
{
    if(U2STAbits.URXDA==1)
    {
        uint8_t tmp = Uart_ReadByte();
        switch(dataIndex)
        {
            case 0:
                if(tmp==g_Bootloader_Receive_StartFlag[0]) //起始标识1
                {
                    receiveData[0] = g_Bootloader_Receive_StartFlag[0];
                    dataIndex = 1;
                }
                else
                    dataIndex = 0;
                break;
            case 1:
                if(tmp==g_Bootloader_Receive_StartFlag[1]) //起始标识2
                {
                    receiveData[1] = g_Bootloader_Receive_StartFlag[1];
                    dataIndex = 2;
                }
                else
                    dataIndex = 0;
                break;
            case 10:
                if(tmp==g_Bootloader_Receive_EndFlag[0])
                {
                    receiveData[dataIndex] = g_Bootloader_Receive_EndFlag[0];
                    dataIndex = 11;
                }
                else
                    dataIndex = 0;
                break;
            case 11:
                if(tmp==g_Bootloader_Receive_EndFlag[1])
                {
                    receiveData[dataIndex] = g_Bootloader_Receive_EndFlag[1];
                    
                    AfterBootloader_CmdType result = Bootloader_DataParse(receiveData,12);
                    //Uart_WriteByte_g(result);
                    switch(result)
                    {
                        case EntryBootloader:
                            break;
                        case Reset:
                            BOOTLOADER_SOFTWARE_RESET();
                            break;
                        case Data: //数据
                            countTimer = 4001; //出现数据即不跳转到用户程序，避免烧录用户程序时出现异常
                            //UserFlash_EraseIvtAndUserAppBlock();
                            UserFlash_DataParseAddrData(receiveData,12);
                            Uart_WriteBytes_g(g_Bootloader_DataReponse,12);
                            GPIO_LED_D3_Toggle_g();
                            break;
                        case DataEnd: //数据结束
                            GPIO_LED_D4_Enable_g();
                            GPIO_LED_D3_Enable_g();
                            Uart_WriteBytes_g(g_Bootloader_DataProgramEndReponse,12);
                            BOOTLOADER_SOFTWARE_RESET(); //数据刷新完毕，复位
                            break;
                        case CheckBootloader: //回馈正处于Bootloader中
                            Uart_WriteBytes_g(g_Bootloader_CheckBootloaderReponse,12);
                            break;
                        case Erase:
                            UserFlash_EraseIvtAndUserAppBlock();
                            Uart_WriteBytes_g(g_Bootloader_EraseFlashReponse,12);
                            break;
                        default:
                            break;
                    }

                }
                
                dataIndex = 0;
                break;
            default: 
                receiveData[dataIndex] = tmp; dataIndex++;
                break;
        }
    }
}